// packageG/mycomponent/supplyChain/goodsAlbum/goodsAlbum.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  options: {
    multipleSlots: true, // 复数插槽: 是
    addGlobalClass: true,
  },
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
    btnFlag: {
      type: null
    },
    U_tabcontrol: {
      type: null,
    },
    isLast: {
      type: null,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    page:1,
    per_page:15, //每页多少条数据
    arrIndex: 1, //组件加载下一页
    isLoadMore: true,
    isLoadMoreSet:false,
    allLoaded: false, //全部数据加载完成
    totalPage:0,
    allianceUnList:[]
  },
  lifetimes: {
    created() {
    },
    attached: function () {
    }
  },
  observers: {
    datas: function (data) {
      if (this.data.totalPage <= this.data.page) {
        this.setData({
          isLoadMore: false,
          allLoaded: true,
        });
      }
    },
    bottomShowBol: function (flag) {
      if (this.data.U_tabcontrol) {
        if(flag && this.data.isLast && this.data.isLoadMore) {
          this.getMoreData();
        } else {
          this.getMoreDataAll();
        }
      }
      if (flag && this.data.bottomShowBolIndex == this.data.index && this.data.isLoadMore) {
        this.getMoreData();
      } else {
        this.getMoreDataAll();
      }
    },
  },

  /**
   * 组件的方法列表
   */
  ready() {
    this.setListData('platformDataList');
  },
  methods: {
    subplatformTo(event) {
      let id = event.currentTarget.dataset.activityid;
      wx.navigateTo({
        url:`/packageJ/unionCps/transfer/transfer?activ=${id}`
      })
    },
    setListData(setList) { //处理列表数据是自动获取还是手动获取
      let dataList = this.data.datas.list.data;
      let allianceArr = [];
      if (Array.isArray(dataList)) {
        this.setData({
          totalPage: this.data.datas.list.last_page,
        });
        if (this.data.totalPage > this.data.page) {
          this.setData({
            isLoadMore: true,
          });
        }
        if( dataList.length > this.data.datas.get_number) { //大于后台控制的数量就显示后台数量列表
          dataList.splice(this.data.datas.get_number,dataList.length-this.data.datas.get_number);
          this.setData({
            isLoadMore:false
          })
        }
        this.setData({
          [setList]: dataList,
        });
      } else {
        if (this.data.datas.list.length <= 0) {
          this.setData({
            isLoadMore: false,
          });
        }
        if(this.data.datas.list.length > this.data.per_page) {
          this.data.isLoadMoreSet = true;
        }
        if(Array.isArray(this.data.datas.list)) {
          allianceArr = this.data.datas.list.slice(0, 15)
        }
        this.setData({
          [setList]: allianceArr || [],
          allianceUnList:this.data.datas.list,
          isLoadMore:false,
          isLoadMoreSet:this.data.isLoadMoreSet
        });
      }
    },
    getMoreDataAll() {
      if (this.data.allianceUnList.slice(this.data.per_page * this.data.arrIndex).length > 0) {
        this.data.platformDataList = this.data.platformDataList.concat(this.data.allianceUnList.slice(this.data.per_page * this.data.arrIndex, this.data.per_page * (this.data.arrIndex + 1))); //数组拼接
        this.setData({
          platformDataList: this.data.platformDataList,
          arrIndex:this.data.arrIndex + 1
        })
        if(this.data.allianceUnList.length === this.data.platformDataList.length) {
          this.setData({
            isLoadMoreSet: false,
          })
        }
      }
    },
    getMoreData() {
      this.setData({
        isLoadMore:false
      })
      if (this.data.page >= this.data.totalPage) {
        this.setData({
          isLoadMore:false
        })
        return;
      } else {
        this.data.page = this.data.page + 1;
        let json = {
          page:this.data.page,
          decorate_id:this.data.page_id,
          component_id:this.data.component_id,
          component_key:'U_unionCps',
        }
        let urlStr = app.getNetAddresss("home-page.get-decorate-page");
        app._postNetWork({
          url: urlStr,
          data:json,
          success:  (resdata) => {
            let res = resdata.data;
            let list = res.data.data;
            if (res.result == 1) {
              this.data.totalPage <= this.data.page?this.data.isLoadMore = false:this.data.isLoadMore =true;
              this.setData({
                isLoadMore:this.data.isLoadMore,
                platformDataList : this.data.platformDataList.concat(list),
              })
            } else {
              this.data.page = this.data.page - 1;
              this.setData({
                isLoadMore:false
              })
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          },
          fail: function (res) {
            console.log(res);
          }
        });
      }
    }
  }
})
